package com.gitee.sop.sopauth.controller;

import com.alibaba.fastjson.JSONObject;
import com.gitee.sop.mongodb.MongoConstants;
import com.gitee.sop.mongodb.MongoDTO;
import com.gitee.sop.mongodb.MongoDbResult;
import com.gitee.sop.mongodb.MongoLike;
import com.gitee.sop.servercommon.annotation.Open;
import com.gitee.sop.servercommon.bean.OpenContext;
import com.gitee.sop.servercommon.bean.ServiceContext;
import com.gitee.sop.servercommon.exception.ServiceException;
import com.gitee.sop.servercommon.result.R;
import com.gitee.sop.servercommon.util.StringUtils;
import com.gitee.sop.sopauth.auth.OAuth2Service;
import com.gitee.sop.sopauth.entity.UserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.Set;

/**
 * 用户信息
 *
 * @author admin
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private OAuth2Service oAuth2Service;

    /**
     * 请求样例
     * <p>
     * {
     * "current":1,
     * "size":10,
     * "sortParams":{
     * "registerTime":"desc"
     * },
     * "queryParams":[
     * {
     * "match":"=",
     * "key":"appId",
     * "value":"2cec228d27e6493fb8f26811540ce319"
     * },
     * {
     * "match":"in",
     * "key":"phone",
     * "valueList":["15123309443"]
     * },
     * {
     * "match":">=",
     * "key":"registerTime",
     * "value":1614762226790
     * },
     * {
     * "match":"<=",
     * "key":"registerTime",
     * "value":1614762226888
     * }
     * ]
     * }
     */
    @ApiOperation(value = "分页查询门户用户", notes = "分页查询门户用户")
    @Open(value = "open.auth.user.searchPage")
    @PostMapping("/search/page")
    public Object searchPage(@RequestBody MongoDTO mongoDTO) {
        OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
        String appId = openContext.getAppId();

        Set<MongoLike> queryParams = mongoDTO.getQueryParams();
        if (null == queryParams) {
            queryParams = new HashSet<>();
        }
        MongoLike mongoLike = new MongoLike();
        mongoLike.setKey("appId");
        mongoLike.setValue(appId);
        mongoLike.setMatch(MongoConstants.query1);
        queryParams.add(mongoLike);
        mongoDTO.setQueryParams(queryParams);
        MongoDbResult mongoDbResult = oAuth2Service.queryPageByParam(mongoDTO);
        return R.ok(mongoDbResult);
    }


    @ApiOperation(value = "查询指定门户用户", notes = "查询指定门户用户")
    @Open(value = "open.auth.user.detail")
    @GetMapping("/detail")
    public Object detail(String userUuid) {
        if (StringUtils.isBlank(userUuid)) {
            throw new ServiceException("用户唯一主键标识不能为空");
        }
        JSONObject jsonObject = oAuth2Service.searchOne(userUuid);
        return R.ok(jsonObject);
    }

    @ApiOperation(value = "修改指定用户状态", notes = "修改指定用户状态")
    @Open(value = "open.auth.user.changeStatus")
    @PutMapping("/changeStatus")
    public Object update(@RequestBody UserInfo portalUser) {
        if (StringUtils.isBlank(portalUser.getUuid()) || StringUtils.isBlank(portalUser.getStatus())) {
            throw new ServiceException("参数校验失败");
        }
        oAuth2Service.updateOne(portalUser.getUuid(), portalUser.getStatus());
        return R.ok();
    }

    @ApiOperation(value = "删除指定门户用户", notes = "删除指定门户用户")
    @Open(value = "open.auth.user.delete")
    @DeleteMapping("/delete")
    public Object delete(String userUuid) {
        if (StringUtils.isBlank(userUuid)) {
            throw new ServiceException("用户唯一主键标识不能为空");
        }
        oAuth2Service.deleteOne(userUuid);
        return R.ok();
    }
}
